package com.todod.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.todod.annotaion.NotLogging;
import com.todod.dto.ApiDto;
import com.todod.entity.ResultData;
import com.todod.mapper.DataDictoryFileMapper;
import com.todod.mapper.ShuoMingFileMapper;
import com.todod.mapper.UsersManualFileMapper;
import com.todod.model.BaseNotice;
import com.todod.model.DataDictoryCatalog;
import com.todod.model.DataDictoryFile;
import com.todod.model.Dict;
import com.todod.model.ShuoMingCatalog;
import com.todod.model.ShuoMingFile;
import com.todod.model.UsersManualCatalog;
import com.todod.model.UsersManualFile;
import com.todod.service.BaseNoticeSercice;
import com.todod.service.IDataDictoryCatalogService;
import com.todod.service.IDataDictoryFileService;
import com.todod.service.IDictService;
import com.todod.service.IUsersManualCatalogService;
import com.todod.service.IUsersManualFileService;
import com.todod.service.ProjectInfoService;
import com.todod.service.ShuoMingCatalogService;
import com.todod.service.ShuoMingFileService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@RestController
@RequestMapping("/api")
@Transactional
public class ApiController extends BaseManageController {
	@Autowired
	public ShuoMingCatalogService shuoMingCatalogService;
	@Autowired
	private ShuoMingFileService shuomingFileService;
	@Autowired
	private ShuoMingFileMapper shuomingFileMapper;
	@Autowired
	public ProjectInfoService projectInfoService;
	@Autowired
	private BaseNoticeSercice baseNoticeSercice;
	@Autowired
	public IUsersManualCatalogService usersManualCatalogService;
	@Autowired
	private IUsersManualFileService usersManualFileService;
	@Autowired
	private UsersManualFileMapper usersManualFileMapper;
	@Autowired
	public IDataDictoryCatalogService dataDictoryCatalogService;
	@Autowired
	private IDataDictoryFileService dataDictoryFileService;
	@Autowired
	private DataDictoryFileMapper dataDictoryFileMapper;
	@Autowired
	private IDictService dictService;

	/**
	 * @Title: getXMList
	 * @Description:根据客户id获得项目信息
	 * @author dyl
	 * @date 2020-12-26
	 * @return
	 */
	@GetMapping("/getXMList")
	@NotLogging
	public ResultData getXMList() {

		List<ApiDto> list = projectInfoService.getXMList();

		return ResultData.success("ok", list);
	}

	/**
	 * @Title: getAPIList
	 * @Description:根据客户id获得项目信息
	 * @author dyl
	 * @date 2020-12-26
	 * @param xmId
	 * @return
	 */
	@GetMapping("/getAPIList")
	@NotLogging
	public ResultData getAPIList(String xmId) {

		List<ShuoMingCatalog> list = shuoMingCatalogService.getListByParentId(Long.valueOf(xmId));// 一级菜单

		List<ApiDto> apiList = new ArrayList<ApiDto>();

		for (ShuoMingCatalog groupTable : list) {// 循环一级菜单增加二级菜单
			ApiDto apiDto = new ApiDto();

			apiDto.setId(groupTable.getId());
			apiDto.setTitle(groupTable.getName());
			apiDto.setFlag("ML");

			JSONObject jo = new JSONObject();
			JSONArray ja = JSONArray.fromObject(jo);
			ja.remove(0);

			List<ShuoMingCatalog> zList = shuoMingCatalogService.getListByParentId(groupTable.getId());// 二级菜单
			if (zList != null && zList.size() > 0) {
				for (ShuoMingCatalog groupTable2 : zList) {
					jo = new JSONObject();
					jo.put("id", groupTable2.getId());
					jo.put("title", groupTable2.getName());
					jo.put("parentid", groupTable.getId());
					jo.put("parentname", groupTable.getName());
					jo.put("flag", "ML");

					List<ShuoMingCatalog> zzList = shuoMingCatalogService.getListByParentId(groupTable2.getId());// 三级菜单

					JSONObject jo1 = new JSONObject();
					JSONArray ja1 = JSONArray.fromObject(jo1);
					ja1.remove(0);

					if (zzList != null && zzList.size() > 0) {// 判断是否有三级菜单
						for (ShuoMingCatalog groupTable4 : zzList) {// 循环三级菜单增加四级菜单

							List<ApiDto> apiList2 = new ArrayList<ApiDto>();
							List<ShuoMingFile> list2 = shuomingFileService.getListByClassifyId(groupTable4.getId());

							for (ShuoMingFile ShuoMingFile : list2) {
								ApiDto apiDto2 = new ApiDto();

								apiDto2.setId(ShuoMingFile.getId());
								apiDto2.setTitle(ShuoMingFile.getTableName());
								apiDto2.setParentid(groupTable4.getId());
								apiDto2.setParentname(groupTable4.getName());
								apiDto2.setFlag("WD");

								apiList2.add(apiDto2);
							}

							jo1 = new JSONObject();
							jo1.put("id", groupTable4.getId());
							jo1.put("title", groupTable4.getName());
							jo1.put("parentid", groupTable2.getId());
							jo1.put("parentname", groupTable2.getName());
							jo1.put("flag", "ML");
							jo1.put("children", apiList2);
							ja1.add(jo1);
						}
					} else {
						List<ShuoMingFile> list2 = shuomingFileService.getListByClassifyId(groupTable2.getId());
						for (ShuoMingFile shuoMingFile : list2) {// 循环三级菜单增加四级菜单
							jo1 = new JSONObject();
							jo1.put("id", shuoMingFile.getId());
							jo1.put("title", shuoMingFile.getTableName());
							jo1.put("parentid", groupTable2.getId());
							jo1.put("parentname", groupTable2.getName());
							jo1.put("flag", "WD");

							ja1.add(jo1);
						}
					}

					jo.put("children", ja1);
					ja.add(jo);

					apiDto.setChildren(ja);
				}
			} else {
				List<ShuoMingFile> list2 = shuomingFileService.getListByClassifyId(groupTable.getId());

				for (ShuoMingFile ShuoMingFile : list2) {

					jo = new JSONObject();
					jo.put("id", ShuoMingFile.getId());
					jo.put("title", ShuoMingFile.getTableName());
					jo.put("parentid", groupTable.getId());
					jo.put("parentname", groupTable.getName());
					jo.put("flag", "WD");
					ja.add(jo);
				}

				apiDto.setChildren(ja);
			}

			apiList.add(apiDto);
		}

		return ResultData.success("ok", apiList);
	}

	/**
	 * @Title: getAPIMXList
	 * @Description:根据客户id获得项目信息
	 * @author dyl
	 * @date 2020-12-26
	 * @param xmId
	 * @return
	 */
	@GetMapping("/getAPIMXList")
	@NotLogging
	public ResultData getAPIMXList(String id) {

		ShuoMingFile shuoMingFile = shuomingFileMapper.selectById(Long.valueOf(id));

		ApiDto apiDto = new ApiDto();

		apiDto.setId(shuoMingFile.getId());
		apiDto.setTitle(shuoMingFile.getTableName());

		String update = shuoMingFile.getCreatetimeTi().toString();
		apiDto.setUptime(update.substring(0, 10).replace("-", "."));
		apiDto.setContent(shuoMingFile.getHtmlData());

		return ResultData.success("ok", apiDto);
	}

	/**
	 * @Title: getManualList
	 * @Description:根据项目id获取用户手册
	 * @author dyl
	 * @date 2022-08-05
	 * @param xmId
	 * @return
	 */
	@GetMapping("/getManualList")
	@NotLogging
	public ResultData getManualList(String xmId) {

		List<UsersManualCatalog> list = usersManualCatalogService.getListByParentId(Long.valueOf(xmId));// 一级菜单

		List<ApiDto> apiList = new ArrayList<ApiDto>();

		for (UsersManualCatalog groupTable : list) {// 循环一级菜单增加二级菜单
			ApiDto apiDto = new ApiDto();

			apiDto.setId(groupTable.getId());
			apiDto.setTitle(groupTable.getName());
			apiDto.setFlag("ML");

			JSONObject jo = new JSONObject();
			JSONArray ja = JSONArray.fromObject(jo);
			ja.remove(0);

			List<UsersManualCatalog> zList = usersManualCatalogService.getListByParentId(groupTable.getId());// 二级菜单
			if (zList != null && zList.size() > 0) {
				for (UsersManualCatalog groupTable2 : zList) {
					jo = new JSONObject();
					jo.put("id", groupTable2.getId());
					jo.put("title", groupTable2.getName());
					jo.put("parentid", groupTable.getId());
					jo.put("parentname", groupTable.getName());
					jo.put("flag", "ML");

					List<UsersManualCatalog> zzList = usersManualCatalogService.getListByParentId(groupTable2.getId());// 三级菜单

					JSONObject jo1 = new JSONObject();
					JSONArray ja1 = JSONArray.fromObject(jo1);
					ja1.remove(0);

					if (zzList != null && zzList.size() > 0) {// 判断是否有三级菜单
						for (UsersManualCatalog groupTable4 : zzList) {// 循环三级菜单增加四级菜单

							List<ApiDto> apiList2 = new ArrayList<ApiDto>();
							List<UsersManualFile> list2 = usersManualFileService
									.getListByClassifyId(groupTable4.getId());

							for (UsersManualFile UsersManualFile : list2) {
								ApiDto apiDto2 = new ApiDto();

								apiDto2.setId(UsersManualFile.getId());
								apiDto2.setTitle(UsersManualFile.getTableName());
								apiDto2.setParentid(groupTable4.getId());
								apiDto2.setParentname(groupTable4.getName());
								apiDto2.setFlag("WD");

								apiList2.add(apiDto2);
							}

							jo1 = new JSONObject();
							jo1.put("id", groupTable4.getId());
							jo1.put("title", groupTable4.getName());
							jo1.put("parentid", groupTable2.getId());
							jo1.put("parentname", groupTable2.getName());
							jo1.put("flag", "ML");
							jo1.put("children", apiList2);
							ja1.add(jo1);
						}
					} else {
						List<UsersManualFile> list2 = usersManualFileService.getListByClassifyId(groupTable2.getId());
						for (UsersManualFile shuoMingFile : list2) {// 循环三级菜单增加四级菜单
							jo1 = new JSONObject();
							jo1.put("id", shuoMingFile.getId());
							jo1.put("title", shuoMingFile.getTableName());
							jo1.put("parentid", groupTable2.getId());
							jo1.put("parentname", groupTable2.getName());
							jo1.put("flag", "WD");

							ja1.add(jo1);
						}
					}

					jo.put("children", ja1);
					ja.add(jo);

					apiDto.setChildren(ja);
				}
			} else {
				List<UsersManualFile> list2 = usersManualFileService.getListByClassifyId(groupTable.getId());

				for (UsersManualFile UsersManualFile : list2) {

					jo = new JSONObject();
					jo.put("id", UsersManualFile.getId());
					jo.put("title", UsersManualFile.getTableName());
					jo.put("parentid", groupTable.getId());
					jo.put("parentname", groupTable.getName());
					jo.put("flag", "WD");
					ja.add(jo);
				}

				apiDto.setChildren(ja);
			}

			apiList.add(apiDto);
		}

		return ResultData.success("ok", apiList);
	}

	/**
	 * @Title: getManualMXList
	 * @Description:根据手册id获得明细信息
	 * @author dyl
	 * @date 2022-08-05
	 * @param xmId
	 * @return
	 */
	@GetMapping("/getManualMXList")
	@NotLogging
	public ResultData getManualMXList(String id) {

		UsersManualFile shuoMingFile = usersManualFileMapper.selectById(Long.valueOf(id));

		ApiDto apiDto = new ApiDto();

		apiDto.setId(shuoMingFile.getId());
		apiDto.setTitle(shuoMingFile.getTableName());

		String update = shuoMingFile.getCreatetimeTi().toString();
		apiDto.setUptime(update.substring(0, 10).replace("-", "."));
		apiDto.setContent(shuoMingFile.getHtmlData());

		return ResultData.success("ok", apiDto);
	}

	/**
	 * @Title: getDictListByCode
	 * @Description:根据父字典code获取获取数据字典集合
	 * @author MengHaoPeng
	 * @date 2019-10-09
	 * @return ResultData
	 */
	@NotLogging
	@RequestMapping("/getDictListByCode")
	public ResultData getDictListByCode(String code) {
		List<Dict> list = dictService.getDictListByCode2(code);
		return ResultData.success("ok", list);
	}

	/**
	 * @Title: getNoticeList
	 * @Description:根据项目id和公告类型获得项目信息
	 * @author dyl
	 * @date 2022-08-02
	 * @param xmId 项目id
	 * @param type 公告类型
	 * @return
	 */
	@GetMapping("/getNoticeList")
	@NotLogging
	public ResultData getNoticeList(String xmId, String type) {

		List<BaseNotice> list = baseNoticeSercice.getNoticeList(xmId, type);
		return ResultData.success("ok", list);
	}

	/**
	 * @Title: getNoticeInfo
	 * @Description:根据公告id获得公告信息
	 * @author dyl
	 * @date 2022-08-02
	 * @return
	 * @param noticeId 公告id
	 * @return
	 */
	@GetMapping("/getNoticeInfo")
	@NotLogging
	public ResultData getNoticeInfo(String noticeId) {

		BaseNotice info = baseNoticeSercice.getById(noticeId);
		return ResultData.success("ok", info);
	}

	/**
	 * @Title: getManualList
	 * @Description:根据项目id获取用户手册
	 * @author dyl
	 * @date 2022-08-05
	 * @param xmId
	 * @return
	 */
	@GetMapping("/getDataDictoryList")
	@NotLogging
	public ResultData getDataDictoryList(String xmId) {

		List<DataDictoryCatalog> list = dataDictoryCatalogService.getListByParentId(Long.valueOf(xmId));// 一级菜单

		List<ApiDto> apiList = new ArrayList<ApiDto>();

		for (DataDictoryCatalog groupTable : list) {// 循环一级菜单增加二级菜单
			ApiDto apiDto = new ApiDto();

			apiDto.setId(groupTable.getId());
			apiDto.setTitle(groupTable.getName());
			apiDto.setFlag("ML");

			JSONObject jo = new JSONObject();
			JSONArray ja = JSONArray.fromObject(jo);
			ja.remove(0);

			List<DataDictoryCatalog> zList = dataDictoryCatalogService.getListByParentId(groupTable.getId());// 二级菜单
			if (zList != null && zList.size() > 0) {
				for (DataDictoryCatalog groupTable2 : zList) {
					jo = new JSONObject();
					jo.put("id", groupTable2.getId());
					jo.put("title", groupTable2.getName());
					jo.put("parentid", groupTable.getId());
					jo.put("parentname", groupTable.getName());
					jo.put("flag", "ML");

					List<DataDictoryCatalog> zzList = dataDictoryCatalogService.getListByParentId(groupTable2.getId());// 三级菜单

					JSONObject jo1 = new JSONObject();
					JSONArray ja1 = JSONArray.fromObject(jo1);
					ja1.remove(0);

					if (zzList != null && zzList.size() > 0) {// 判断是否有三级菜单
						for (DataDictoryCatalog groupTable4 : zzList) {// 循环三级菜单增加四级菜单

							List<ApiDto> apiList2 = new ArrayList<ApiDto>();
							List<DataDictoryFile> list2 = dataDictoryFileService
									.getListByClassifyId(groupTable4.getId());

							for (DataDictoryFile DataDictoryFile : list2) {
								ApiDto apiDto2 = new ApiDto();

								apiDto2.setId(DataDictoryFile.getId());
								apiDto2.setTitle(DataDictoryFile.getTableName());
								apiDto2.setParentid(groupTable4.getId());
								apiDto2.setParentname(groupTable4.getName());
								apiDto2.setFlag("WD");

								apiList2.add(apiDto2);
							}

							jo1 = new JSONObject();
							jo1.put("id", groupTable4.getId());
							jo1.put("title", groupTable4.getName());
							jo1.put("parentid", groupTable2.getId());
							jo1.put("parentname", groupTable2.getName());
							jo1.put("flag", "ML");
							jo1.put("children", apiList2);
							ja1.add(jo1);
						}
					} else {
						List<DataDictoryFile> list2 = dataDictoryFileService.getListByClassifyId(groupTable2.getId());
						for (DataDictoryFile shuoMingFile : list2) {// 循环三级菜单增加四级菜单
							jo1 = new JSONObject();
							jo1.put("id", shuoMingFile.getId());
							jo1.put("title", shuoMingFile.getTableName());
							jo1.put("parentid", groupTable2.getId());
							jo1.put("parentname", groupTable2.getName());
							jo1.put("flag", "WD");

							ja1.add(jo1);
						}
					}

					jo.put("children", ja1);
					ja.add(jo);

					apiDto.setChildren(ja);
				}
			} else {
				List<DataDictoryFile> list2 = dataDictoryFileService.getListByClassifyId(groupTable.getId());

				for (DataDictoryFile DataDictoryFile : list2) {

					jo = new JSONObject();
					jo.put("id", DataDictoryFile.getId());
					jo.put("title", DataDictoryFile.getTableName());
					jo.put("parentid", groupTable.getId());
					jo.put("parentname", groupTable.getName());
					jo.put("flag", "WD");
					ja.add(jo);
				}

				apiDto.setChildren(ja);
			}

			apiList.add(apiDto);
		}

		return ResultData.success("ok", apiList);
	}

	/**
	 * @Title: getDataDictoryMXList
	 * @Description:根据手册id获得明细信息
	 * @author dyl
	 * @date 2022-08-05
	 * @param xmId
	 * @return
	 */
	@GetMapping("/getDataDictoryMXList")
	@NotLogging
	public ResultData getDataDictoryMXList(String id) {

		DataDictoryFile shuoMingFile = dataDictoryFileMapper.selectById(Long.valueOf(id));

		ApiDto apiDto = new ApiDto();

		apiDto.setId(shuoMingFile.getId());
		apiDto.setTitle(shuoMingFile.getTableName());

		String update = shuoMingFile.getCreateTime().toString();
		apiDto.setUptime(update.substring(0, 10).replace("-", "."));

		apiDto.setContent(shuoMingFile.getHtmlData().replaceAll("<table", "<table border='1' "));

		return ResultData.success("ok", apiDto);
	}

}